package top.dancun.easuser.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Getter;
import lombok.Setter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.dancun.common.entity.RoleEntity;
import top.dancun.common.entity.UserEntity;
import top.dancun.common.utils.Constant;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;
import top.dancun.easuser.entity.PassWordVojo;
import top.dancun.easuser.feign.StudentFeign;
import top.dancun.easuser.feign.TeacherFeign;
import top.dancun.easuser.service.UserService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author dancun
 * @email message@dancun.top
 * @date 2022-09-05 22:15:48
 */
@RestController
@RequestMapping("/user")
public class UserController extends AbstractController{
    @Autowired
    private UserService userService;
    
    @Autowired
    private TeacherFeign teacherFeign;
    @Autowired
    private StudentFeign studentFeign;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("easuser:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("data", page);
    }

    @RequestMapping("/info")
    public R list(){
        UserEntity user = userService.getUserInfo(getUserId());
        List<RoleEntity> roles = user.getRoles();

        JSONObject data = new JSONObject();
        data.put("user",user);

        R r = null;
        for(RoleEntity role :roles){
            if (role.getRoleId().equals(Constant.STUDENT_ROLE_ID)){
                // student模块独立出去
                r =  studentFeign.getStudent(user.getUid());
                if ("20000".equals(r.get("code").toString())){
                    data.put("student",r.get("student"));
                }else{
                    System.err.println(r.get("message"));
                }
            } else if (role.getRoleId().equals(Constant.TEACHER_ROLE_ID)) {
                // student模块独立出去
                r = teacherFeign.getTeacher(user.getUid());
                if ("20000".equals(r.get("code").toString())){
                    data.put("teacher",r.get("teacher"));
                }else{
                    System.err.println(r.get("message"));
                }
            }
        }
        return R.ok().put("data", data);


    }

    @RequestMapping("/allinfo")
    public R listall(){
        List<UserEntity> user = userService.listAllUserAndRole(new QueryWrapper<>());

        return R.ok().put("data", user);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{uid}")
    @RequiresPermissions("easuser:user:info")
    public R info(@PathVariable("uid") String uid){
		UserEntity user = userService.getUserInfo(uid);
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("easuser:user:save")
    public R save(@RequestBody UserEntity user){
		userService.saveUser(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("easuser:user:update")
    public R update(@RequestBody UserEntity user){
		userService.updateUser(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("easuser:user:delete")
    public R delete(@RequestBody String[] uids){
		userService.removeAllByIds(Arrays.asList(uids));

        return R.ok();
    }

    @RequestMapping("/password")
    public R password(@RequestBody PassWordVojo passWordVojo) throws Exception {
        UserEntity user = getUser();
        userService.updatepwd(user,passWordVojo);



        return R.ok();
    }



}
